package com.example.administrator.ygddriver.order.specific;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.ygddriver.R;
import com.example.administrator.ygddriver.base.BaseActivity;
import com.example.administrator.ygddriver.base.BaseBean;
import com.example.administrator.ygddriver.base.UserBean;
import com.example.administrator.ygddriver.liteorm.LiteOrmTools;
import com.example.administrator.ygddriver.map.ReachDestinationRequestBean;
import com.example.administrator.ygddriver.netty.TcpClient;
import com.example.administrator.ygddriver.tool.CustomPopWindow;
import com.example.administrator.ygddriver.tool.OkGoRequests;
import com.example.administrator.ygddriver.tool.SimpleToolbar;
import com.example.administrator.ygddriver.tool.UrlValuse;

import java.util.HashMap;

/**
 * 快递列表 司机接单详情
 */
public class SpecificActivity extends BaseActivity {
    private RecyclerView mSpecificRv;
    private SpecificAdapter mSpecificAdapter;
    private SimpleToolbar simpleToolbar;
    private String branches;
    private OrderActivityReceiver orderActivityReceiver;
    private CustomPopWindow mCustomPopWindow;
    private String token, id;

    @Override
    public int initLayout() {
        return R.layout.activity_specific;
    }

    @Override
    public void initView() {
        orderActivityReceiver = new OrderActivityReceiver();
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(UrlValuse.QR_JJ);
        registerReceiver(orderActivityReceiver, myIntentFilter);

        simpleToolbar = findViewById(R.id.activity_specific_stb);
        mSpecificRv = findViewById(R.id.activity_specific_rv);
        mSpecificAdapter = new SpecificAdapter(R.layout.item_activity_specific, null);
        View view = LayoutInflater.from(this).inflate(R.layout.item_specific_adapter_empty, null);
        mSpecificAdapter.setEmptyView(view);
        mSpecificRv.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        mSpecificRv.setAdapter(mSpecificAdapter);
    }

    @Override
    public void initDate() {
        TcpClient instance_t = TcpClient.getInstance();
        instance_t.sendMsgByByete();
        branches = getIntent().getStringExtra("branches");
        LiteOrmTools.getInformation(UserBean.class, this, new LiteOrmTools.On<UserBean>() {
            @Override
            public void hasData(UserBean data) {
                token = data.getC_token();
                id = data.getC_id();
                initPickUpInfoHtml(branches, data.getC_id(), null, null, data.getC_token());
            }
        }, new LiteOrmTools.OFF() {
            @Override
            public void noData() {

            }
        });

        setSimpleToolbar();

    }

    private void setSimpleToolbar() {
        simpleToolbar.setMainTitle("快递列表");
        simpleToolbar.setMainTitleColor("#000000");
        simpleToolbar.setBackIv(R.mipmap.left_title);
        simpleToolbar.setToolColor("#ffffff");
        simpleToolbar.setBackIvClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initPickUpInfoHtml(String c_branches, String c_driver, String limit, String offset, String c_token) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("c_branches", c_branches);
        hashMap.put("c_driver", c_driver);
        hashMap.put("limit", limit);
        hashMap.put("offset", offset);
        hashMap.put("c_token", c_token);
        hashMap.put("c_type", "2");
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_DRIVER + UrlValuse.PICK_UP_INFO, hashMap, PickUpInfoRequestBean.class, new OkGoRequests.OnTrue<PickUpInfoRequestBean>() {
            @Override
            public void hasData(PickUpInfoRequestBean date) {
                mSpecificAdapter.setNewData(date.getResult());
            }
        }, new OkGoRequests.OnError<PickUpInfoRequestBean>() {
            @Override
            public void noData() {

            }
        });
    }

    public class OrderActivityReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String num = intent.getStringExtra("num");
            setPopW(getWindow().getDecorView(), num);

        }
    }

    private void setPopW(View v, String num) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_activity_specific_popw, null);
        handleLogic(view, num);
        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(this).setView(view)
                .size(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)//显示大小
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .enableOutsideTouchableDissmiss(false)// 设置点击PopupWindow之外的地方，popWindow不关闭，如果不设置这个属性或者为true，则关闭
                .setAnimationStyle(R.style.CustomPopWindowStyle) // 添加自定义显示和消失动画
                .create()
                .showAtLocation(v, Gravity.CENTER, 0, 0);
    }

    private void handleLogic(View view, final String num) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.item_activity_specific_popw_on_tv:
                        initDriverSureTook(num, token);

                        break;
                    case R.id.item_activity_specific_popw_off_tv:
                        mCustomPopWindow.dissmiss();
                        break;
                }
            }
        };
        TextView numTv = view.findViewById(R.id.item_activity_specific_popw_titl_tv);
        TextView onTv = view.findViewById(R.id.item_activity_specific_popw_on_tv);
        TextView offTv = view.findViewById(R.id.item_activity_specific_popw_off_tv);
        numTv.setText(num);
        offTv.setOnClickListener(listener);
        onTv.setOnClickListener(listener);

    }

    //司机端确认揽件
    private void initDriverSureTook(String c_waybill, String c_token) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("c_waybill", c_waybill);
        hashMap.put("c_token", c_token);
        hashMap.put("c_type", UrlValuse.TYPE);
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_DRIVER + UrlValuse.DRIVER_SURE_TOOK, hashMap, BaseBean.class, new OkGoRequests.OnTrue<BaseBean>() {
            @Override
            public void hasData(BaseBean date) {
                if (date.isSuccess()) {
                    Toast.makeText(SpecificActivity.this, date.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                    initPickUpInfoHtml(branches, id, null, null, token);
                } else {
                    Toast.makeText(SpecificActivity.this, date.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                }
                mCustomPopWindow.dissmiss();

            }
        }, new OkGoRequests.OnError<BaseBean>() {
            @Override
            public void noData() {

            }
        });
    }


}
